﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BasicClass;
using System.Threading;
using System.Threading.Tasks;

namespace DealPLC
{
    /// <summary>
    /// WinLogReadWrite.xaml 的交互逻辑
    /// </summary>
    public partial class WinLogReadWrite : BaseWindow
    {
        #region 窗体单实例
        private static WinLogReadWrite g_WinLogReadWrite = null;
        public static WinLogReadWrite GetWinInst(out bool blNew)
        {
            blNew = false;
            try
            {
                if (g_WinLogReadWrite == null)
                {
                    blNew = true;
                    g_WinLogReadWrite = new WinLogReadWrite();
                }
                return g_WinLogReadWrite;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static WinLogReadWrite GetWinInst()
        {
            try
            {
                return g_WinLogReadWrite;
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion 窗体单实例

        #region 定义
        //Timer
        System.Windows.Forms.Timer Tm250_Refresh = new System.Windows.Forms.Timer();//刷新定时器

        int Num = 0;

        bool blAutoRefresh = true;
        #endregion 定义

        #region 初始化
        public WinLogReadWrite()
        {
            InitializeComponent();
            Tm250_Refresh.Interval = 250;
            this.Tm250_Refresh.Tick += Tm250_Refresh_Tick;
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BaseWindow_Loaded(object sender, RoutedEventArgs e)
        {
            new Task(new Action(()=>
            {
                Thread.Sleep(500);
                this.Dispatcher.Invoke(new Action(()=>
                {
                    Tm250_Refresh.Enabled = true;
                }));

            })).Start();
        }
        #endregion 初始化


        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Tm250_Refresh_Tick(object sender, EventArgs e)
        {
            try
            {
                if(this.Visibility==Visibility.Hidden
                    || !blAutoRefresh)
                {                    
                    return;
                }
                //Thread.Sleep(1000);
                ShowPar_Invoke();
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }

        /// <summary>
        /// 是否自动刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkRefresh_Unchecked(object sender, RoutedEventArgs e)
        {
            if(chkRefresh.IsMouseOver)
            {
                blAutoRefresh = false;
            }
        }

        /// <summary>
        /// 是否自动刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkRefresh_Checked(object sender, RoutedEventArgs e)
        {
            if (chkRefresh.IsMouseOver)
            {
                blAutoRefresh = true;
            }
        }

        #region 显示
        public override void ShowPar()
        {
            try
            {
                RefreshDg();
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }

        /// <summary>
        /// 刷新列表
        /// </summary>
        public void RefreshDg()
        {
            try
            {
                int num = 0;
                List<ParLogReadWritePLC> parLogReadWritePLC_L = new List<ParLogReadWritePLC>();
                for (int i = 0; i < ParLogReadWritePLC.P_I.ParLogReadWritePLC_L.Count; i++)
                {
                    ParLogReadWritePLC inst = new ParLogReadWritePLC();
                    if (ParLogReadWritePLC.P_I[i].Time != null)
                    {
                        inst.No = ParLogReadWritePLC.P_I[i].No;
                        inst.Reg = ParLogReadWritePLC.P_I[i].Reg.Replace("\n", "\\n");
                        inst.Data = ParLogReadWritePLC.P_I[i].Data;
                        inst.Co = ParLogReadWritePLC.P_I[i].Co;
                        inst.Type = ParLogReadWritePLC.P_I[i].Type;
                        inst.Annotation = ParLogReadWritePLC.P_I[i].Annotation;
                        inst.Time = ParLogReadWritePLC.P_I[i].Time;

                        num = inst.No;
                    }
                    parLogReadWritePLC_L.Add(inst);
                }

                //如果数据不更新，就不刷新
                if (num == Num)
                {
                    return;
                }
                Num = num; ;

                dgReadAndWrite.ItemsSource = parLogReadWritePLC_L;
                dgReadAndWrite.Items.Refresh();
                if (parLogReadWritePLC_L.Count > 0)
                {
                    dgReadAndWrite.ScrollIntoView(parLogReadWritePLC_L[parLogReadWritePLC_L.Count - 1], dgReadAndWrite.Columns[0]);
                }
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }

        #endregion 显示

        #region 关闭
        private void BaseWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                e.Cancel = true;
                this.Visibility = Visibility.Hidden;
                //默认恢复自动刷新
                chkRefresh.IsChecked = true;
                blAutoRefresh = true;
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }


        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        #endregion 关闭

       

    }
}
